<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Activity;
use App\Models\ActivityCategorys;
use App\Models\Activitys;
use App\Models\Users;
use App\Models\Wechat;
use Illuminate\Http\Request;

class ActivityController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(Request $request){
        $params = [
            'page' => 1,
            'kwd' => '',
            'status' => 0,
            'cid' => 0,
            'start' => '',
            'end' => '',
        ];
        //如果从代办点击过来，则显示待审核
        $from = $request->get('from',0);
        if ($from == 1){
            $params['status'] = 2;
            $from = 0;
        }
        $params = json_encode($params);
        //获取分类
        $categorys = ActivityCategorys::getCascaderList();
        $categorys = json_encode($categorys);
        return view('admin.activity.index',compact('categorys','params'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'cid' => (int)$request->get('cid',0),
                'status' => (int)$request->get('status',0),
                'charge' => (int)$request->get('charge',0),
                'source' => (int)$request->get('source',0),
                'hot' => (int)$request->get('hot',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
                'sort' => (string)$request->get('sort',''),
            ];
            $lists = Activitys::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = Activitys::findOrFail($id);
                $info['cate_id'] = [$info['cate_pid'],$info['cate_id']];
            }
            $info = json_encode($info);

            //获取分类
            $categorys = ActivityCategorys::getCascaderList();
            $categorys = json_encode($categorys);

            return view('admin.activity.activity_update',compact('info','id','categorys'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Activity $request
     * @return bool
     */
    public function update(Activity $request)
    {
        try{
            $data = $request->all();
            $data['cate_pid'] = $data['cate_id'][0];
            $cate_id = 0;
            if (isset($data['cate_id'][1])){
                $cate_id = $data['cate_id'][1];
            }
            $data['cate_id'] = $cate_id;
            if($data['id']){
                $info = Activitys::findOrFail($data['id']);
                $info->update($data);
                return $this->writeJson(0,null,'修改信息成功');
            }else{
                unset($data['id']);
                Activitys::create($data);
                return $this->writeJson(0,null,'新增信息成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 锁定
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Activitys::findOrFail($id);
            $info->update(['status' => Activitys::STATUS_3]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 激活
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Activitys::findOrFail($id);
            $info->update(['status' => Activitys::STATUS_1]);
            //审核通过、推送微信消息
            $user_info = Users::where(['id'=>$info['user_id']])->where('status','<>',-1)->first(['id','mobile','username','openid']);
            if (!empty($user_info['openid'])){
                $template_id = '9dp0uDLyRbtzu2q9NjDvs6M12rPBhZ_p3-k7xzNfHaQ';
                $url = 'http://wap.hrqidian.com/#/myActive';
                $data = [
                    'first' => '亲爱的供应商，您的活动已完成审核',
                    'keyword1' => $user_info['username'],
                    'keyword2' => $user_info['mobile'],
                    'keyword3' => '审核通过',
                    'keyword4' => '请登陆个人中心查看',
                    'remark' => 'HR小启客服',
                ];
                Wechat::wechatSend($user_info['openid'], $template_id, $url, $data);
            }
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 取消活动
     * @param Request $request
     * @return bool
     */
    public function cancel(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Activitys::findOrFail($id);
            $info->update(['status' => Activitys::STATUS_4]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Activitys::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
